package com.jic.market.controller.rest;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.api.rest.GrouponRestApi;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.request.rest.bargain.MyBargainResponse;
import com.jic.market.request.rest.groupon.*;
import com.jic.market.response.rest.groupon.*;
import com.jic.market.service.rest.groupon.GrouponRestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author xuyongliang
 * @version V1.0
 * @className GrouponRestController
 * @description 拼团 rest 接口
 * @date 2020/4/2
 */
@RestController
@RequestMapping("/rest/groupon")
@Api(tags = {"拼团H5接口"})
@Slf4j
public class GrouponRestController implements GrouponRestApi {

    @Resource
    private GrouponRestService grouponRestService;

    @PostMapping("/groupon-list")
    @ApiOperation("拼团活动列表H5")
    public RestResult<PageResult<GrouponInfoResponse>> grouponList(
            @RequestBody GrouponListRequest request) {
        log.info("------进入拼团活动列表H5--参数为:{}-----",request);
        RestResult<PageResult<GrouponInfoResponse>> pageResultRestResult =
                grouponRestService.grouponList(request);
        log.info("------拼团活动列表H5--返回为:{}-----",pageResultRestResult);
        return pageResultRestResult;
    }


    @PostMapping("/groupon-info")
    @ApiOperation("拼团活动详情H5")
    public RestResult<GrouponInfoResponse> grouponInfo(
            @RequestBody @Valid GrouponInfoRequest request) {
        log.info("------进入拼团活动详情H5--参数为:{}-----",request);
        RestResult<GrouponInfoResponse> result = grouponRestService.grouponInfo(request);
        log.info("------拼团活动详情H5--返回为:{}-----",result);
        return result;
    }

    @ApiOperation("开参团活动校验H5")
    @Override
    public RestResult<LaunchOrJoinGrouponResponse> launchOrJoinGrouponCheck(@RequestBody LaunchOrJoinGrouponRequest request) {
        log.info("------开参团活动校验H5--参数为:{}-----",request);
        LaunchOrJoinGrouponResponse innerResult = null;
        try {
            innerResult = grouponRestService.launchOrJoinGrouponCheck(request);
        }catch (MarketException e){
            log.error("开参团活动校验异常",e);
            return RestResult.error(e.getCode(),e.getMessage());
        }catch (Exception e){
            log.error("开参团活动校验未知异常",e);
            return RestResult.error(MarketErrorCodeEnum.GROUP_LAUNCH_JOIN_CHECK.getCode(),e.getMessage());
        }
        log.info("------开团团活动校验H5--返回为:{}-----",innerResult);
        return RestResult.success(innerResult);
    }

    @ApiOperation("开参团活动H5")
    @Override
    public RestResult<LaunchOrJoinGrouponResponse> launchOrJoinGroupon(@RequestBody LaunchOrJoinGrouponRequest request) {
        log.info("------开参团活动H5--参数为:{}-----",request);
        LaunchOrJoinGrouponResponse innerResult = null;
        try {
            innerResult = grouponRestService.launchOrJoinGroupon(request);
        }catch (MarketException e){
            log.error("开参团活动异常",e);
            return RestResult.error(e.getCode(),e.getMessage());
        }catch (Exception e){
            log.error("开参团活动未知异常",e);
            return RestResult.error(MarketErrorCodeEnum.GROUP_LAUNCH_JOIN.getCode(),e.getMessage());
        }
        log.info("------开团团活动H5--返回为:{}-----",innerResult);
        return RestResult.success(innerResult);
    }


    @ApiOperation("查询开团详情H5")
    @Override
    public RestResult<LaunchGrouponDetailResponse> launchGrouponDetail(LaunchGrouponDetailRequest request) {
        log.info("------开团团活动H5--入参为:{}-----",request);
        RestResult<LaunchGrouponDetailResponse> restResult = grouponRestService.launchGrouponDetail(request);
        log.info("------开团团活动H5--出参为:{}-----",restResult);
        return restResult;
    }

    @ApiOperation("查询砍价商品详情H5")
    @Override
    public RestResult<List<MyBargainResponse>> launchGrouponProductDetail(MyBargainRequest request) {
        log.info("------砍价商品详情H5--入参为:{}-----",request);
        RestResult<List<MyBargainResponse>> restResult = grouponRestService.launchGrouponProductDetail(request);
        log.info("------砍价商品详情H5--出参为:{}-----",restResult);
        return restResult;
    }


    @PostMapping("/current-joined-groupon-list")
    @ApiOperation("查询当前活动已开团未成团的列表H5")
    public RestResult<PageResult<CurrentJoinedGrouponResponse>> currentJoinedGrouponList(
            @RequestBody CurrentJoinedGrouponRequest request) {
        log.info("------查询当前活动已开团未成团的列表H5--参数为:{}-----",request);
        RestResult<PageResult<CurrentJoinedGrouponResponse>> pageResultRestResult =
                grouponRestService.currentJoinedGrouponList(request);
        log.info("------查询当前活动已开团未成团的列表H5--返回为:{}-----",pageResultRestResult);
        return pageResultRestResult;
    }

    @PostMapping("/post-pay-group-inst-detail")
    @ApiOperation("拼团订单支付同步H5")
    public RestResult<PostPayGroupInstDetailResponse> postPayGroupInstDetail(
            @RequestBody PostPayGroupInstDetailRequest request) {
        log.info("------拼团订单支付同步H5--参数为:{}-----",request);
        PostPayGroupInstDetailResponse response = grouponRestService.postPayGroupInstDetail(request);
        log.info("------拼团订单支付同步H5--返回为:{}-----",response);
        return RestResult.success(response);
    }

    @PostMapping("/cancelPayGroupInstDetail")
    @ApiOperation("拼团订单取消H5")
    public RestResult<PostPayGroupInstDetailResponse> cancelPayGroupInstDetail(
            @RequestBody CancelPayGroupInstDetailRequest request) {
        log.info("------拼团订单取消H5--参数为:{}-----",request);
        PostPayGroupInstDetailResponse response = grouponRestService.cancelPayGroupInstDetail(request);
        log.info("------拼团订单取消H5--返回为:{}-----",response);
        return RestResult.success(response);
    }


    @PostMapping("/queryGroupProductList")
    @ApiOperation("查询进行中的拼团活动商品列表-分页")
    public RestResult<PageResult<QueryGroupProductListResponse>> queryGroupProductList(@RequestBody QueryGroupProductListRequest request){
        return grouponRestService.queryGroupProductList(request);
    }

    @PostMapping("/getCurrentJoinedGroupInfo")
    @ApiOperation("前端-查询拼团详情")
    public RestResult<CurrentJoinedGroupResponse> getCurrentJoinedGroupInfo(
            @RequestBody CurrentJoinedGrouponRequest request) {
        log.info("------前端-查询拼团详情--参数为:{}-----", request);
        return grouponRestService.getCurrentJoinedGroupInfo(request);
    }

    @PostMapping("/getMyGroupList")
    @ApiOperation("前端-我的拼团活动列表")
    public RestResult<PageResult<GroupInfoResponse>> getMyGroupList(@RequestBody GrouponListRequest request) {
        return grouponRestService.getMyGroupList(request);
    }

    @PostMapping("/queryLaunchGrouponOrderInfo")
    @ApiOperation("前端-根据订单号查询拼团实例详情")
    public RestResult<LaunchGrouponDetailResponse> queryLaunchGrouponOrderInfo(@RequestBody LaunchGrouponDetailRequest request){
        return grouponRestService.queryLaunchGrouponOrderInfo(request);
    }

}
